/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cn.icanci.tomato.common;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.MDC;

/**
 * 日志记录和发送模块
 * @author icanci
 * @date Created in 2021/04/11 15:24
 * @version 1.0
 */
public class LogUtil {
    private static final String MODULE        = "LOGGER_MODULE";
    private static final String CATEGORY      = "LOGGER_CATEGORY";
    private static final String SUB_CATEGORY  = "LOGGER_SUB_CATEGORY";
    private static final String PREFIX_MODULE = "TICKET_";

    private LogUtil() {
    }

    public static void info(Logger logger, String format, Object... objects) {
        logger.info(format, objects);
    }

    public static void warn(Logger logger, String format, Object... objects) {
        logger.warn(format, objects);
    }

    public static void error(Logger logger, String format, Object... objects) {
        logger.error(format, objects);
    }

    public static void warn(Logger logger, String format, Throwable throwable, Object... objects) {
        logger.warn(format, throwable, objects);
    }

    public static void error(Logger logger, String format, Throwable throwable, Object... objects) {
        logger.error(format, throwable, objects);
    }

    public static void init(String module, String category, String subCategory) {
        setModule(module);
        setCategory(category);
        setCategory(subCategory);
    }

    public static void setModule(String module) {
        if (StringUtils.isNotBlank(module) && !StringUtils.equals("null", module)) {
            if (StringUtils.startsWith(module, PREFIX_MODULE)) {
                MDC.put(MODULE, module);
            } else {
                MDC.put(MODULE, PREFIX_MODULE + module);
            }
        } else {
            removeModule();
        }
    }

    public static String getModule() {
        return MDC.get(MODULE);
    }

    public static void setCategory(String category) {
        if (StringUtils.isNotBlank(category) && !StringUtils.equals("null", category)) {
            if (StringUtils.startsWith(category, PREFIX_MODULE)) {
                MDC.put(CATEGORY, category);
            } else {
                MDC.put(CATEGORY, PREFIX_MODULE + category);
            }
        } else {
            removeCategory();
        }
    }

    public static String getCategory() {
        return MDC.get(CATEGORY);
    }

    public static void setSubCategory(String subCategory) {
        if (StringUtils.isNotBlank(subCategory) && !StringUtils.equals("null", subCategory)) {
            if (StringUtils.startsWith(subCategory, PREFIX_MODULE)) {
                MDC.put(SUB_CATEGORY, subCategory);
            } else {
                MDC.put(SUB_CATEGORY, PREFIX_MODULE + subCategory);
            }
        } else {
            removeSubCategory();
        }
    }

    public static String getSubCategory() {
        return MDC.get(SUB_CATEGORY);
    }

    public static void removeAll() {
        removeModule();
        removeCategory();
        removeSubCategory();
    }

    public static void removeSubCategory() {
        MDC.remove(SUB_CATEGORY);
    }

    public static void removeCategory() {
        MDC.remove(CATEGORY);
    }

    public static void removeModule() {
        MDC.remove(MODULE);
    }
}
